import { _decorator, Vec3, Mesh, utils, Color, v3 } from 'cc';
import { Visualization } from '../libs/utils/Visualization/Visualization';
const { ccclass, property, executeInEditMode } = _decorator;

declare const CC_EDITOR;

@ccclass('EditorGrid')
@executeInEditMode
export class EditorGrid extends Visualization {

    @property
    private _unit: number = .1;
    @property
    public get unit(): number { return this._unit; }
    public set unit(v: number) { this._unit = v; this._modelChanged = true; }

    protected _updateMesh(mesh: Mesh) {
        let positions: number[] = [], colors: number[] = [];

        let color0 = [.1, .1, .1, 1];
        let color1 = [.6, .4, .4, 1];
        let color2 = [.7, .7, .7, 1];

        function linkPoints(p0: Vec3, p1: Vec3, col: number[]) {
            if (!(p0 && p1)) return;
            Vec3.toArray(positions, p0, positions.length);
            Vec3.toArray(positions, p1, positions.length);
            colors.push(col[0], col[1], col[2], col[3]);
            colors.push(col[0], col[1], col[2], col[3]);
        }

        for (let i = -37; i <= 37; i++) {
            let color = (i % 10 == 0) ? color0 : ((i % 5 == 0) ? color1 : color2);
            linkPoints(v3(i * this.unit, 2, -7), v3(i * this.unit, 2, 7), color);
        }
        for (let i = -54; i <= 54; i++) {
            let color = (i % 10 == 0) ? color0 : ((i % 5 == 0) ? color1 : color2);
            linkPoints(v3(-5, 2, i * this.unit), v3(5, 2, i * this.unit), color);
        }

        return utils.MeshUtils.createMesh({ positions, colors }, mesh);
    }
}
